package operate

import (
	"gin-demo/common"
	"gin-demo/container"
	"gin-demo/service/operate"
	"github.com/gin-gonic/gin"
	"strconv"
)

func AddOperateRouter(engine *gin.Engine) {

	group := engine.Group("operate")
	{
		group.GET("/record/list", recordList)
		group.GET("/user/list", userList)
	}
}

// @Summary 管控接口
// @Description 获取集群列表
// @Tags operate
// @Accept application/json
// @Produce application/json
// @Success 200 {object} common.Response
// @Failure 400 {object} common.Response
// @Failure 500 {object} common.Response
// @Router /operate/record/list [get]
func recordList(context *gin.Context) {

	pageNum, _ := strconv.Atoi(context.Query("pageNum"))
	pageSize, _ := strconv.Atoi(context.Query("pageSize"))

	var pageRequest common.PageRequest
	pageRequest.PageNum = pageNum
	pageRequest.PageSize = pageSize
	service := container.Get("recordService").(operate.RecordServiceImp)

	list, err := service.List(pageRequest)
	response := common.NewCommonResponse()
	if err != nil {
		response.Code = common.CodeRecordListFail
		response.Message = common.MsgRecordListFail
	}

	response.Data = list

	context.JSON(response.Code, response)
}

// @Summary 管控接口
// @Description 获取集群列表
// @Tags operate
// @Accept application/json
// @Produce application/json
// @Success 200 {object} common.Response
// @Failure 400 {object} common.Response
// @Failure 500 {object} common.Response
// @Router /operate/user/list [get]
func userList(context *gin.Context) {

	pageNum, _ := strconv.Atoi(context.Query("pageNum"))
	pageSize, _ := strconv.Atoi(context.Query("pageSize"))

	var pageRequest common.PageRequest
	pageRequest.PageNum = pageNum
	pageRequest.PageSize = pageSize
	service := container.Get("userService").(operate.UserServiceImp)

	list, err := service.List(pageRequest)
	response := common.NewCommonResponse()
	if err != nil {
		response.Code = common.CodeUserListFail
		response.Message = common.MsgUserListFail
	}

	response.Data = list

	context.JSON(response.Code, response)
}
